package com.zxy.controlcenter.core.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoleilu.hutool.convert.Convert;
import com.xiaoleilu.hutool.util.StrUtil;
import com.zxy.controlcenter.core.http.SpringContextHolder;
import com.zxy.controlcenter.modular.system.model.Dictionary;
import com.zxy.controlcenter.modular.system.model.Role;
import com.zxy.controlcenter.modular.system.repository.DictionaryMapper;
import com.zxy.controlcenter.modular.system.repository.RoleMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 常量的生产工厂
 *
 * @author zxy
 */
@Component
@DependsOn("springContextHolder")
public class ConstantFactory {

    private final RoleMapper roleMapper;
    private final DictionaryMapper dictionaryMapper;

    public ConstantFactory(RoleMapper roleMapper, DictionaryMapper dictionaryMapper) {
        this.roleMapper = roleMapper;
        this.dictionaryMapper = dictionaryMapper;
    }

    public static ConstantFactory me() {
        return SpringContextHolder.getBean("constantFactory");
    }

    /**
     * 获取权限名称集合
     *
     * @param roleIds 权限id集合
     * @return 权限名称集合
     */
    public String getRoleName(String roleIds) {
        if (StringUtils.isBlank(roleIds)) {
            return "";
        }
        Long[] roles = Convert.toLongArray(roleIds);
        StringBuilder sb = new StringBuilder();
        for (Long role : roles) {
            Role roleObj = roleMapper.selectById(role);
            if (roleObj != null && StringUtils.isNotBlank(roleObj.getRoleName())) {
                sb.append(roleObj.getRoleName()).append(",");
            }
        }
        return StrUtil.removeSuffix(sb.toString(), ",");
    }

    /**
     * 获取权限名称
     *
     * @param roleId 权限id
     * @return 权限名称
     */
    public String getSingleRoleName(Long roleId) {
        if (0 == roleId) {
            return "--";
        }
        Role roleObj = roleMapper.selectById(roleId);
        if (roleObj != null && StringUtils.isNotBlank(roleObj.getRoleName())) {
            return roleObj.getRoleName();
        }
        return "";
    }

    /**
     * 获取字典信息
     *
     * @param dictId 字典id
     * @return 字典信息
     */
    public Dictionary getDict(Long dictId) {
        if (dictId != null) {
            return dictionaryMapper.selectById(dictId);
        }
        return null;
    }

    /**
     * 获取字典信息集合
     *
     * @param dictionary 字典查询参数
     * @return 字典信息集合
     */
    public List<Dictionary> getDicList(Dictionary dictionary, String desc) {
        List<Dictionary> dictionaryList = new ArrayList<>();
        if (dictionary != null) {
            QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(dictionary.getName())) {
                queryWrapper.eq("name", dictionary.getName());
            }
            if (StringUtils.isNotBlank(dictionary.getDescription())) {
                queryWrapper.eq("description", dictionary.getDescription());
            }
            if (StringUtils.isNotBlank(dictionary.getDictCode())) {
                queryWrapper.eq("dict_code", dictionary.getDictCode());
            }
            if (StringUtils.isNotBlank(dictionary.getDictType())) {
                queryWrapper.eq("dict_type", dictionary.getDictType());
            }
            if (dictionary.getStatus() != null) {
                queryWrapper.eq("status", dictionary.getStatus());
            }
            if (StringUtils.isNotBlank(desc) && "desc".equals(desc)) {
                queryWrapper.orderByDesc("sort");
            }
            dictionaryList = dictionaryMapper.selectList(queryWrapper);
        }
        return dictionaryList;
    }
}
